﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net.Nntp
{
    using System;
    using System.IO;
    using System.Net;

    /// <summary>Represents a client connection to an NNTP server.</summary>
    /// <threadsafety static="true" instance="false"/>
    public sealed class Client : ClientBase<Client>
    {
        private readonly bool postingAllowed;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Initializes a new instance of the <see cref="Client"/> class, by calling
        /// <see cref="Client(string, int)"/>(hostName, 119).</summary>
        public Client(string hostName) : this(hostName, 119)
        {
        }

        /// <summary>Initializes a new instance of the <see cref="Client"/> class.</summary>
        /// <param name="hostName">The DNS name of the NNTP server to connect to.</param>
        /// <param name="port">The port number of the NNTP server to connect to (usually 119).</param>
        /// <exception cref="ArgumentNullException"><paramref name="hostName"/> equals <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not between
        /// <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
        /// <exception cref="ServiceUnavailableException">The host answered that the service is not available.
        /// </exception>
        /// <exception cref="IOException">A general I/O error occurred, see <see cref="Exception.InnerException"/>
        /// for more information.</exception>
        /// <exception cref="UnexpectedResponseException">The response received during initial connection to the server
        /// was unexpected.</exception>
        public Client(string hostName, int port) : base(hostName, port, new QuitCommand())
        {
            try
            {
                this.postingAllowed = new ConnectCommand().Execute(this).PostingAllowed;
            }
            catch
            {
                this.Dispose();
                throw;
            }
        }

        /// <summary>Gets a value indicating whether posting is currently allowed.</summary>
        public bool PostingAllowed
        {
            get { return !this.IsDisposed && this.postingAllowed; }
        }
    }
}
